﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ханой
{
    class Program
    {
        static void Main(string[] args)
        {
            Start:
            int[] tower1 = { 3, 2, 1 };
            int[] tower2 = { 0, 0, 0 };
            int[] tower3 = { 0, 0, 0 };
            int moveCounter = 0;
            do
            {
                moveCounter++;
                Console.Clear();
                Console.WriteLine("Ханойские башни");
                Console.WriteLine("___________________________________");
                Console.WriteLine("");
                Console.WriteLine(" " + tower1[2] + "   " + tower2[2] + "   " + tower3[2]);
                Console.WriteLine(" " + tower1[1] + "   " + tower2[1] + "   " + tower3[1]);
                Console.WriteLine(" " + tower1[0] + "   " + tower2[0] + "   " + tower3[0]);
                Console.WriteLine("--- --- ---");
                Console.WriteLine("Номер башни, с которой надо снять кольцо");
                int startTower = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Номер башни, на которую нужно поместить кольцо");
                int finishTower = Convert.ToInt32(Console.ReadLine());


                int temp = 0;
                if (startTower == 1)
                {
                    for (int i = 2; temp == 0; i--)
                    {
                        if (tower1[i] != 0)
                        {
                            temp = tower1[i];
                            tower1[i] = 0;
                        }
                    }
                }
                if (startTower == 2)
                {
                    for (int i = 2; temp == 0; i--)
                    {
                        if (tower2[i] != 0)
                        {
                            temp = tower2[i];
                            tower2[i] = 0;
                        }
                    }
                }
                if (startTower == 3)
                {
                    for (int i = 2; temp == 0; i--)
                    {
                        if (tower3[i] != 0)
                        {
                            temp = tower3[i];
                            tower3[i] = 0;
                        }
                    }
                }
               
                if (finishTower == 1)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (tower1[i] == 0)
                        {
                            tower1[i] = temp;
                            temp = 0;
                            if (tower1[2] > tower1[1] || tower1[1] > tower1[0])
                            {
                                Console.WriteLine("Перечитай правила!");
                                Console.ReadLine();
                                goto Start;
                            }
                        }
                    }
                }
                if (finishTower == 2)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (tower2[i] == 0)
                        {
                            tower2[i] = temp;
                            temp = 0;
                            if (tower2[2] > tower2[1] || tower2[1] > tower2[0])
                            {
                                Console.WriteLine("Перечитай правила!");
                                Console.ReadLine();
                                goto Start;
                            }
                        }
                    }
                }
                if (finishTower == 3)
                {
                    for (int i = 0; i < 3; i++)
                    {
                        if (tower3[i] == 0)
                        {
                            tower3[i] = temp;
                            temp = 0;
                            if (tower3[2] > tower3[1] || tower3[1] > tower3[0])
                            {
                                Console.WriteLine("Перечитай правила!");
                                Console.ReadLine();
                                goto Start;
                            }
                        }
                    }
                }
            }
            while (tower3[0] != 3 || tower3[1] != 2 || tower3[2] != 1);
            Console.Clear();
            Console.WriteLine("Ханойские башни");
            Console.WriteLine("___________________________________");
            Console.WriteLine("");
            Console.WriteLine(" " + tower1[2] + "   " + tower2[2] + "   " + tower3[2]);
            Console.WriteLine(" " + tower1[1] + "   " + tower2[1] + "   " + tower3[1]);
            Console.WriteLine(" " + tower1[0] + "   " + tower2[0] + "   " + tower3[0]);
            Console.WriteLine("--- --- ---");
            Console.WriteLine("  Победа!!!");
            Console.WriteLine("c " + moveCounter + " попыток");
            Console.ReadLine();
            
        }
    }
}
